﻿using System;
using System.Collections.Generic;
using IRepository;//引用接口
using WorkModel;
using Dapper;
using Microsoft.Extensions.Configuration;//拓展配置
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Repository//[实现接口类]
{
    //UserinfouRepository实现用户接口
    public class UserinfouRepository : IUserinfouRepository//继承用户接口
    {
        //全局变量 相当于上下文1
        public IDbConnection conn;
        //依赖注入
        private string mc = "";//定义一个连接字符串
        //依赖注入包
        private IConfiguration ma = null;
        //构造函数
        public UserinfouRepository(IConfiguration mb)//ctorTT
        {
            //把依赖注入的包注入到构造函数里
            ma = mb;
            //转换SQL语句
            Dapper.SimpleCRUD.SetDialect(SimpleCRUD.Dialect.SQLServer);
            //连接字符串
            mc = ma.GetConnectionString("DBconn");
            //相当于上下文2
            conn = new SqlConnection(mc);
        }
        /// <summary>
        /// 实现用户表显示
        /// </summary>
        /// <param name="name"></param>
        /// <param name="cIndex"></param>
        /// <param name="pSize"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<Userinfou> UserShow(string name, int cIndex, int pSize, out int Count)
        {
            //计算总条数
            Count = conn.RecordCount<Userinfou>();
            if (name != "")
            {
                string sql = $"select* from Userinfou where Uname = '{name}'";
                return conn.Query<Userinfou>(sql).ToList();
            }
            //分页
            return conn.GetListPaged<Userinfou>(cIndex, pSize, "", "").ToList();
        }
        /// <summary>
        /// 实现用户表注册
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int UserAdd(Userinfou m)
        {
            return (int)conn.Insert<Userinfou>(m);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UserDelete(string id)
        {
            return conn.Delete<Userinfou>(id);
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int UserUpt(Userinfou m)
        {
            return conn.Update<Userinfou>(m);
        }
        /// <summary>
        /// 用户反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Userinfou UserFan(int id)
        {
            return conn.Get<Userinfou>(id);
        }
        /// <summary>
        /// 用户唯一性查询
        /// </summary>
        /// <param name="checkname"></param>
        /// <returns></returns>
        public List<Userinfou> UserCheck(  string name)
        {
            string f = $"select * from Userinfou where UserName= '{name}'";
            return conn.Query<Userinfou>(f).ToList();
           
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public int UserLongin(string UserName, string UserPwd)
        {
            string n = $"select count(*) from Userinfou where UserName ='{UserName}'and UserPwd='{UserPwd}'";

            return conn.ExecuteScalar<int>(n);
        }
    }
}
